//
var dopBtnorderqueryAdd = "";
var mapManager = null;
var isModify = false;
var rowPosion=0;
$("#dop_datagrid_orderList").datagrid({
	url : "",
	height : 350,
	pagination : true,
	fitColumns : false,
	singleSelect : false,
	fit : true,
	pageSize : 10,
	pageList : [10, 30, 50, 100],
	sortOrder : 'desc',
	rownumbers : true,
	showFooter : true,
	frozenColumns : [[]],
	columns : [[{
		field : 'ck',
		checkbox : true,
		hidden : true
	}, {
		field : "id",
		width : 150,
		title : "主键",
		hidden : true
	}, {
		field : "code",
		width : 150,
		title : "订单号",
		align:'center',
		formatter:function(value,row,index){
			return "<a href='javascript:void(0);'  style='color:red;'>"+value+"</a>"
		}
	}, {
		field : "statusText",
		width : 120,
		title : "状态",
		align:'center',
		formatter:function(value,row,index){
			if(row.readyStatus==1&&row.status==3){
				return value+"("+row.readyStatusStr+")";
			}else if(row.readyStatus==2&&row.status==4){
				return value+"("+row.readyStatusStr+")";
			}
			return value;
		}
	},{
		field : "deliveryManName",
		width : 80,
		title : "员工姓名",
		align:'center'
	},{
		field : "detail",
		width : 250,
		title : "订单明细",
		halign:"center",
		align:'left'
	}, {
		field : "custName",
		width : 100,
		title : "客户姓名",
		align:'center'
	}, {
		field : "telephone",
		width : 150,
		title : "电话",
		halign:"center",
		align:'left'
	}, {
		field : "custAddress",
		width : 280,
		title : "客户地址",
		halign:"center",
		align:'left'
	},{
		field : "createDate",
		width : 120,
		title : "下单日期",
		align:'center',
		formatter:function(value,row,index){
			return new Date(value).format("yyyy-MM-dd hh:mm");
		}
	}, {
		field : "askSendTime",
		width : 120,
		title : "要求送达时间",
		align:'center',
		formatter:function(value,row,index){
			if(value==null){
				return "";
			}
			return new Date(value).format("yyyy-MM-dd hh:mm");
		}
	}
	// , {
	// field : "deliveryTime",
	// width : 150,
	// title : "配送时间"
	// }
	, {
		field : "payMethodText",
		width : 80,
		title : "支付方式",
		align:'center'
	},  {
		field : "totalPrice",
		width : 80,
		title : "订单金额",
		halign:"center",
		align:'right'
	},{
		field : "orderTotalPrice",
		width : 80,
		title : "订单总金额",
		halign:"center",
		align:'right'
	}, 
	/*{
		field : "needWarmBoxText",
		width : 150,
		title : "需要保温箱",
		align:'center'
	},*/
	{
		field : "storefrontName",
		width : 150,
		title : "店面",
		halign:"center",
		align:'left'
	}]],
	/**
	 * 单元格单击事件
	 * @param {} rowIndex
	 * @param {} field
	 * @param {} value
	 */
	onClickCell:function(rowIndex, field, value){
		var row= $('#dop_datagrid_orderList').datagrid('getData').rows[rowIndex];
		rowPosion=rowIndex+1;
		/**
		 * 如果单击了订单号的行，弹出详细信息
		 */
		if(value==row.code){
			showOrderInfo(row);
		}
		
	}
});

 /**
  * 显示订单详情
  * @param {} row
  */
 var orderId=$("#orderId");
 var code=$("#code");
 var source=$("#source");
 var createDate=$("#createDate");
 var custName=$("#custName");
 var telephone=$("#telephone");
 var askSendTime=$("#askSendTime");
 var custAddress=$("#custAddress");
 var orderTotalPrice=$("#orderTotalPrice");
 var deliveryPrice=$("#deliveryPrice");
 var totalPrice=$("#totalPrice");
 var preferentialPrice=$("#preferentialPrice");
  var note=$("#note");
 function showOrderInfo(row){
 	isSingleMakeFood=true;
 	orderId.val(row.id);
 	code.text(row.code);
 	source.text("");
 	createDate.text(new Date(row.createDate).format("yyyy-MM-dd hh:mm"));
 	custName.text(row.custName);
 	telephone.text(row.telephone);
 	askSendTime.text(new Date(row.askSendTime).format("yyyy-MM-dd hh:mm"));
 	custAddress.text(row.custAddress);
 	orderTotalPrice.text(row.orderTotalPrice+"元");
 	deliveryPrice.text(row.deliveryPrice+"元");
 	totalPrice.text(row.totalPrice+"元");
 	preferentialPrice.text("0元");
 	note.html(row.note);
	var foodDetail=$("#foodDetail");
	foodDetail.find("tr:first").nextAll().remove();
 	for(var i=0;i<row.foodDetail.length;i++){
 		var food=row.foodDetail[i];
 		foodDetail.append("<tr><td>"+food.food+"</td><td>"+food.count+"份</td><td>"+food.money+"元</td></tr>");
 	}
 	layerIndex=$.layer({
 		type:1,
 		shadeClose : true,
 		title : '<div><span style="line-height: 14px;">订单信息</span></div>',
		more:true,
		area : [ 1000, 500 ],
		page : {
			dom : '#layerDialog'
		},
		close:function(){
			/**
			 * 关闭后默认选择第一个
			 */
			 $("#layerDialog").tabs("select","订单详情");
			 var record=$("#orderRecord");
 			record.find("tr:eq(1)").show();
		}
 	});
 
 }
 
  /**
  * 上一条单击事件
  */
  $("#proOrder").click(function(){
  	var rowsCount= $('#dop_datagrid_orderList').datagrid("getRows").length;
  	rowPosion-=1;
  	if(rowPosion<=0){
  		rowPosion=0;
  	}
 	var row= $('#dop_datagrid_orderList').datagrid('getData').rows[rowPosion];
 	
 	showOrderInfo(row);
 });
 
 /**
  * 下一条单击事件
  */
 $("#nextOrder").click(function(){
 	var rowsCount= $('#dop_datagrid_orderList').datagrid("getRows").length;
 	rowPosion+=1;
 	if(rowPosion>=rowsCount){
 		rowPosion=rowsCount-1;
 	}
	var row= $('#dop_datagrid_orderList').datagrid('getData').rows[rowPosion];
 	showOrderInfo(row);
 });
 /**
  * tab标签单击事件
  */
 $("#layerDialog").tabs({
 	onSelect:function(title,index){
		if(index==1){
			$("#codeDiv").html("订单编号:"+code.text());
			loadRecord();
		}
 	}
 });
/**
 * 加载订单状态记录
 */
 function loadRecord(){
 	var orderId=$("#orderId").val();
 	$.ajax({
 		url:"storefrontAcceptController.do?loadRecord",
 		type:"post",
 		data:{orderId:orderId},
 		dataType:"json",
 		cache:false,
 		success:function(data,textStatus){
 			var record=$("#orderRecord");
 			record.find("tr:eq(1)").nextAll().remove();
 			var recordStr="";
 			for(var i=0;i<data.length;i++){
 				recordStr+="<tr><td style='text-align: center;vertical-align: middle;'>"+data[i].changTimeStr+
 					"</td><td style='text-align: left;vertical-align: middle;'>"+data[i].recordDesc+"</td></tr>";
 			}
 			record.find("tr:eq(1)").hide();
 			record.append(recordStr);
 		},
 		error:function(textStatus, errorThrown){
			
 		}
 	});
 }
// click pagination
var p = $("#dop_datagrid_orderList").datagrid('getPager');
$(p).pagination({
	onSelectPage : function(pageNumber, pageSize)
	{
		var params = $.extend({
			pageNum : pageNumber,
			pageSize : pageSize
		}, getQueryParams());
		EapTools.doAction("orderQueryController.do?query", params, true, function(data)
		{
			$("#dop_datagrid_orderList").datagrid('loadData', {
				"rows" : data.list,
				"total" : data.count
			} || {});
		});
	}
});

// bind query event
$("#dop_btn_orderquery_query").click(function(){
	init();
});

init();
// get params
function getQueryParams()
{
	return {
		code : $("input[code='code']").val(),
		deliveryManName : $("input[code='deliveryManName']").val(),
		status : $("select[code='status']").val() || 0,
		custName : $("input[code='custName']").val(),
		createDate : $("input[code='createDate']").val(),
		askSendTime:$("input[code='askSendTime']").val(),
		payMethod : $("select[code='payMethod']").val() || 0
	};
}
// init
function init(){
	// init
	var options = $("#dop_datagrid_orderList").datagrid('getPager').data("pagination").options;
	var pageSize = options.pageSize;
	var params = $.extend({
		pageNum : 1,
		pageSize : pageSize
	}, getQueryParams());
	EapTools.doAction("orderQueryController.do?query", params, true, function(data){
		
		$("#dop_datagrid_orderList").datagrid('loadData', {
			"rows" : data.list,
			"total" : data.count
		} || {});
		var p = $("#dop_datagrid_orderList").datagrid('getPager');
		$("td:last", p).find("a").click();
	});
}


/**
 * 格式化日期
 * @param {} fmt
 * @return {}
 */
Date.prototype.Format = function(fmt) 
{ //author: meizz 
  var o = { 
    "M+" : this.getMonth()+1,                 //月份 
    "d+" : this.getDate(),                    //日 
    "h+" : this.getHours(),                   //小时 
    "m+" : this.getMinutes(),                 //分 
    "s+" : this.getSeconds(),                 //秒 
    "q+" : Math.floor((this.getMonth()+3)/3), //季度 
    "S"  : this.getMilliseconds()             //毫秒 
  }; 
  if(/(y+)/.test(fmt)) 
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  for(var k in o) 
    if(new RegExp("("+ k +")").test(fmt)) 
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); 
  return fmt; 
}
